const PI = 3.14;

exports.calculatePrice = (reqData, goods) => {
  // console.log("进来");
  let priceDetails = [];
  for (let i = 0; i < reqData.length; i++) {
    t_detail = [];
    let quoData = reqData[i].detail;

    if (
      !(
        (!isNaN(parseFloat(quoData.specs.length[0])) &&
          !isNaN(parseFloat(quoData.specs.width[0]))) ||
        (!isNaN(parseFloat(quoData.specs.radius[0])) &&
          !isNaN(parseFloat(quoData.basePlate.thickness[0])) &&
          !isNaN(parseFloat(quoData.shroudPlate.thickness[0])))
      )
    ) {
      priceDetails.push([]);
      break;
    }

    // console.log(quoData, "----------------------");
    for (let j = 0; j < goods.length; j++) {
      let priceDetail = {
        basePlate: {},
        shroudPlate: {},
        company: {},
        price: 0,
        amount: parseInt(reqData[i].detail.amount),
        image: "",
      };
      let b_type =
        quoData.basePlate.type[1] == "任意型号"
          ? true
          : quoData.basePlate.type[1] == goods[j].baseplate.type;
      let s_type =
        quoData.shroudPlate.type[1] == "任意型号"
          ? true
          : quoData.shroudPlate.type[1] == goods[j].shroudplate.type;
      // console.log(b_type);
      if (
        quoData.basePlate.category[1] == goods[j].baseplate.category &&
        b_type &&
        quoData.shroudPlate.category[1] == goods[j].shroudplate.category &&
        s_type &&
        goods[j].shape == quoData.specs.shape[1]
      ) {
        // console.log(goods[j].baseplate);
        // console.log(goods[j].shroudplate);
        // console.log("================================");
        let basePlate = {
          category: goods[j].baseplate.category,
          type: goods[j].baseplate.type,
          // name: goods[j].baseplate.category + goods[j].baseplate.type,
          density: goods[j].baseplate.density,
          unit_price: [goods[j].baseplate.m_cost, ""],
          pro_price: [goods[j].b_process.price, ""],
          res_price: 0,
        };
        let shroudPlate = {
          category: goods[j].shroudplate.category,
          type: goods[j].shroudplate.type,
          // name: goods[j].shroudplate.category + goods[j].shroudplate.type,
          density: goods[j].shroudplate.density,
          unit_price: [goods[j].shroudplate.m_cost, ""],
          pro_price: [goods[j].b_process.price, ""],
          res_price: 0,
        };

        // 基板
        // console.log("++++++++++++++ 基板 ++++++++++++++");

        let b_area; // 基板面积
        // let b_weight; // 基板重量
        if (goods[j].shape == "圆形") {
          let radius = parseFloat(quoData.specs.radius[0]);
          if (quoData.specs.radius[1] == 1) {
            radius *= 0.1;
          } else if (quoData.specs.radius[1] == 2) {
            radius *= 0.01;
          }
          b_area = PI * (radius + 0.02) * (radius + 0.02); // 基板面积
        }
        if (goods[j].shape == "长方形") {
          let length = parseFloat(quoData.specs.length[0]);
          if (quoData.specs.length[1] == 1) {
            length *= 0.1;
          } else if (quoData.specs.length[1] == 2) {
            length *= 0.01;
          }
          let width = parseFloat(quoData.specs.width[0]);
          if (quoData.specs.width[1] == 1) {
            width *= 0.1;
          } else if (quoData.specs.width[1] == 2) {
            width *= 0.01;
          }
          b_area = (length + 0.2) * (width + 0.2); // 基板面积
        }
        // console.log("b_area--- ", b_area);

        let b_raw_price = 0; // 基板原料费
        let b_pro_price = 0; // 基板加工费
        let b_price = 0;
        // 按面积购买
        if (goods[j].baseplate.processMode == "1") {
          basePlate.unit_price[1] = "元/㎡";
          b_raw_price = b_area * goods[j].baseplate.m_cost;
          // console.log("基板原料费(面积)--- ", b_raw_price);
        }
        // 按重量购买
        if (goods[j].baseplate.processMode == "0") {
          basePlate.unit_price[1] = "元/kg";
          let thickness = parseFloat(quoData.basePlate.thickness[0]);
          if (quoData.basePlate.thickness[1] == 1) {
            thickness *= 0.1;
          } else if (quoData.shroudPlate.thickness[1] == 2) {
            thickness *= 0.01;
          }
          let b_weight = b_area * thickness * goods[j].baseplate.density * 1000;
          b_raw_price = b_weight * goods[j].baseplate.m_cost;
          // console.log("基板原料费(重量)--- ", b_raw_price);
        }
        // 按面积加工
        if (goods[j].b_process.processMode == "1") {
          basePlate.pro_price[1] = "元/㎡";
          // console.log("++++++++++ 按面积加工 ++++++++++");
          // console.log("基板面积--- ", b_area);
          b_pro_price = b_area * goods[j].b_process.price;
          // console.log("基板加工费--- ", b_pro_price);
        }
        // 按重量加工
        if (goods[j].b_process.processMode == "0") {
          basePlate.pro_price[1] = "元/kg";
          // console.log("++++++++++ 按重量计算 ++++++++++");

          let thickness = parseFloat(quoData.basePlate.thickness[0]);
          if (quoData.basePlate.thickness[1] == 1) {
            thickness *= 0.1;
          } else if (quoData.basePlate.thickness[1] == 2) {
            thickness *= 0.01;
          }
          // console.log(thickness);
          let b_weight = b_area * thickness * goods[j].baseplate.density * 1000;

          // console.log("基板重量--- ", b_weight);
          b_pro_price = b_weight * goods[j].b_process.price;
          // console.log("基板加工费--- ", b_pro_price);
        }
        b_price = b_raw_price + b_pro_price;
        // console.log("基板费用--- ", b_price);
        basePlate.res_price = b_price;

        priceDetail.basePlate = basePlate;

        // 覆板
        // console.log("++++++++++++++ 覆板 ++++++++++++++");

        let s_area; // 覆板面积
        // let s_weight; // 覆板重量
        if (goods[j].shape == "圆形") {
          let radius = parseFloat(quoData.specs.radius[0]);
          if (quoData.specs.radius[1] == 1) {
            radius *= 0.1;
          } else if (quoData.specs.radius[1] == 2) {
            radius *= 0.01;
          }
          s_area = PI * radius * radius; // 覆板面积
        }
        if (goods[j].shape == "长方形") {
          let length = parseFloat(quoData.specs.length[0]);
          if (quoData.specs.length[1] == 1) {
            length *= 0.1;
          } else if (quoData.specs.length[1] == 2) {
            length *= 0.01;
          }
          let width = parseFloat(quoData.specs.width[0]);
          if (quoData.specs.width[1] == 1) {
            width *= 0.1;
          } else if (quoData.specs.width[1] == 2) {
            width *= 0.01;
          }
          s_area = (length + 0.2) * (width + 0.2); // 覆板面积
        }
        // console.log("s_area---", s_area);

        let s_raw_price = 0; // 覆板原料费
        let s_pro_price = 0; // 覆板加工费
        let s_price = 0;
        // 按面积购买
        if (goods[j].shroudplate.processMode == "1") {
          shroudPlate.unit_price[1] = "元/㎡";
          s_raw_price = s_area * goods[j].shroudplate.m_cost;
          // console.log("覆板原料费(面积)--- ", s_raw_price);
        }
        // 按重量购买
        if (goods[j].shroudplate.processMode == "0") {
          shroudPlate.unit_price[1] = "元/kg";
          let thickness = parseFloat(quoData.shroudPlate.thickness[0]);
          if (quoData.shroudPlate.thickness[1] == 1) {
            thickness *= 0.1;
          } else if (quoData.shroudPlate.thickness[1] == 2) {
            thickness *= 0.01;
          }
          let s_weight =
            s_area * thickness * goods[j].shroudplate.density * 1000;
          s_raw_price = s_weight * goods[j].shroudplate.m_cost;
          // console.log("覆板原料费(重量)--- ", s_raw_price);
        }
        // 按面积加工
        if (goods[j].s_process.processMode == "1") {
          shroudPlate.pro_price[1] = "元/㎡";
          // console.log("++++++++++ 按面积加工 ++++++++++");
          // console.log("覆板面积--- ", s_area);
          s_pro_price = s_area * goods[j].s_process.price;
          // console.log("覆板加工费--- ", s_pro_price);
        }
        // 按重量加工
        if (goods[j].s_process.processMode == "0") {
          shroudPlate.pro_price[1] = "元/kg";
          // console.log("++++++++++ 按重量计算 ++++++++++");
          let thickness = parseFloat(quoData.shroudPlate.thickness[0]);
          if (quoData.shroudPlate.thickness[1] == 1) {
            thickness *= 0.1;
          } else if (quoData.shroudPlate.thickness[1] == 2) {
            thickness *= 0.01;
          }
          let s_weight =
            s_area * thickness * goods[j].shroudplate.density * 1000;

          // console.log("覆板重量--- ", s_weight);
          s_pro_price = s_weight * goods[j].s_process.price;
          // console.log("覆板加工费--- ", s_pro_price);
        }
        s_price = s_raw_price + s_pro_price;
        // console.log("覆板费用--- ", s_price);
        shroudPlate.res_price = s_price;

        priceDetail.shroudPlate = shroudPlate;

        priceDetail.price = (b_price + s_price).toFixed(2);

        priceDetail.image = goods[j].image;

        priceDetail.company = {
          company_name: goods[j].companyId.company_name,
          address: goods[j].companyId.address,
          detailed_address: goods[j].companyId.detailed_address,
          admin_name: goods[j].companyId.admin_name,
          admin_phone: goods[j].companyId.admin_phone,
        };

        t_detail.push(priceDetail);
      }
    }
    priceDetails.push(t_detail);
  }
  return priceDetails;
};
